﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DoctorReg
{
    public partial class FormConsultas : Form
    {
        public FormConsultas()
        {
            InitializeComponent();
        }

        private void FormConsultas_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoSizeColumnsMode =
   DataGridViewAutoSizeColumnsMode.AllCells;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
            dataGridView1.EditMode =
            DataGridViewEditMode.EditProgrammatically;
            ConsultasDataSetTableAdapters.ConsultasTableAdapter
            adapterConsultas = new
            ConsultasDataSetTableAdapters.ConsultasTableAdapter();
            ConsultasDataSet.ConsultasDataTable tabConsultas = new
            ConsultasDataSet.ConsultasDataTable();
            adapterConsultas.Fill(tabConsultas);
            ConsultasDataSetTableAdapters.PacientesTableAdapter
            adapterPacientes = new
            ConsultasDataSetTableAdapters.PacientesTableAdapter();
            ConsultasDataSet.PacientesDataTable tabPacientes = new
            ConsultasDataSet.PacientesDataTable();
            adapterPacientes.Fill(tabPacientes);
            ConsultasDataSetTableAdapters.MédicosTableAdapter
            adapterMedicos = new
            ConsultasDataSetTableAdapters.MédicosTableAdapter();
            ConsultasDataSet.MédicosDataTable tabMedicos = new
            ConsultasDataSet.MédicosDataTable();
            adapterMedicos.Fill(tabMedicos);
            foreach (DataColumn coluna in tabConsultas.Columns)
            {
                dataGridView1.Columns.Add(coluna.ColumnName,
                coluna.ColumnName);
            }
            int idxLinha = 0, idxColuna = 0;
            foreach (DataRow linha in tabConsultas.Rows)
            {
                DataGridViewRow linhaDataGrid = new DataGridViewRow();
                dataGridView1.Rows.Add(linhaDataGrid);
                foreach (DataColumn coluna in tabConsultas.Columns)
                {
                    object valor = tabConsultas.Rows[idxLinha][idxColuna];
                    if (idxColuna == 2)
                    {
                        valor = tabPacientes.FindByCodigo((int)valor).Nome;
                    }
                    else if (idxColuna == 3)
                    {
                        valor = tabMedicos.FindByContribuinte((int)valor).Nome;
                    }
                    else
                    {
                        valor = tabConsultas.Rows[idxLinha][idxColuna];
                    }
                    if (idxColuna == 1)
                    {
                        DateTime data =
                        (DateTime)tabConsultas.Rows[idxLinha][idxColuna];
                        dataGridView1.Rows[idxLinha].Cells[idxColuna].Value =
                        data.ToShortDateString();
                    }
                    else
                    {
                        dataGridView1.Rows[idxLinha].Cells[idxColuna].Value =
                        valor;
                    }
                    idxColuna = idxColuna + 1;
                }
                idxLinha = idxLinha + 1;
                idxColuna = 0;
            }

        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int consulta;
            string paciente;
            string medico;
            DateTime data;
            consulta = (int)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
            data = Convert.ToDateTime(dataGridView1.Rows[e.RowIndex].Cells[1].Value);
            paciente =
            dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
            medico =
            dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
            FormConsultas2 form = new FormConsultas2();
            form.Alterar(consulta, data, paciente, medico);
            form.ShowDialog();

        }
    }
}
